热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#|Convert。ToInt64(字符串,表单提供者)方法

C#|Convert。ToInt64(字符串,表单提供者)方法

C# | Convert。ToInt64(字符串,表单提供者)方法

原文:https://www . geesforgeks . org/c-sharp-convert-to int 64 string-iformatprovider-method/

此方法用于使用指定的区域性特定格式信息,将数字的指定字符串表示形式转换为等效的 64 位有符号整数。

语法:

public static long ToInt64 (string value, IFormatProvider provider);

参数:


  • 值:是包含要转换的数字的字符串。

  • 提供程序:它是一个提供区域性特定格式信息的对象。

返回值:该方法返回一个十进制数,它相当于中的数字,如果空值,则返回 0(零)。

异常:


  • 格式异常:如果不是由后跟数字序列(0 到 9)的可选符号组成。

  • overoverowexception:如果代表小于最小值或大于最大值的数字。

以下程序说明了转换的使用。ToInt64(字符串,表单提供者)方法:

例 1:

// C# program to demonstrate the
// Convert.ToInt64() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
    try {
        // creating object of CultureInfo
        CultureInfo cultures = new CultureInfo("en-US");
        // declaring and initializing String array
        string[] values = {"12345", "+12345",
                                   "-12345"};
        // calling get() Method
        Console.Write("Converted long value"
                    + " from a specified string ");
        for (int j = 0; j < values.Length; j++) 
        {
            get(values[j], cultures);
        }
    }
    catch (FormatException e)
    {
        Console.WriteLine("\n");
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (OverflowException e)
    {
        Console.WriteLine("\n");
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
// Defining get() method
public static void get(string s,
           CultureInfo cultures)
{
    // converting string to specified long
    long val = Convert.ToInt64(s, cultures);
    // display the converted long value
    Console.Write(" {0}, ", val);
}
}

Output:

Converted long value from a specified string 12345, 12345, -12345,

例 2:格式异常

// C# program to demonstrate the
// Convert.ToInt64() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
    try {
        // creating object of CultureInfo
        CultureInfo cultures = new CultureInfo("en-US");
        // declaring and initializing String array
        string[] values = {"12345", "+12345",
                                   "-12345"};
        // calling get() Method
        Console.Write("Converted long value"
                    + " of specified strings: ");
        for (int j = 0; j < values.Length; j++) 
        {
            get(values[j], cultures);
        }
        Console.WriteLine("\n");
        string s = "123 456, 789";
        Console.WriteLine("format of s is invalid ");
        // converting string to specified char
        long val = Convert.ToInt64(s, cultures);
        // display the converted char value
        Console.Write(" {0}, ", val);
    }
    catch (FormatException e)
    {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (OverflowException e) 
    {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
// Defining get() method
public static void get(string s,
           CultureInfo cultures)
{
    // converting string to
    // specified long value
    long val = Convert.ToInt64(s, cultures);
    // display the converted
    // decimal value
    Console.Write(" {0}, ", val);
}
}

Output:

Converted long value of specified strings: 12345, 12345, -12345,
format of s is invalid
Exception Thrown: System.FormatException

例 3: 适用于飞越异常

// C# program to demonstrate the
// Convert.ToInt64() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
    try {
        // creating object of CultureInfo
        CultureInfo cultures = new CultureInfo("en-US");
        // declaring and initializing String array
        string[] values = {"12345", "+12345",
                                   "-12345"};
        // calling get() Method
        Console.Write("Converted long value "
                 + "of specified strings: ");
        for (int j = 0; j < values.Length; j++)
        {
            get(values[j], cultures);
        }
        Console.WriteLine("\n");
        string s = "-7922816251426433759354395033500000";
        Console.WriteLine("s is less than the MinValue");
        // converting string to specified long
        long val = Convert.ToInt64(s, cultures);
        // display the converted char value
        Console.Write(" {0}, ", val);
    }
    catch (FormatException e)
    {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
    catch (OverflowException e) 
    {
        Console.Write("Exception Thrown: ");
        Console.Write("{0}", e.GetType(), e.Message);
    }
}
// Defining get() method
public static void get(string s,
           CultureInfo cultures)
{
    // converting string to
    // specified long value
    long val = Convert.ToInt64(s, cultures);
    // display the converted long value
    Console.Write(" {0}, ", val);
}
}

Output:

Converted long value of specified strings: 12345, 12345, -12345,
s is less than the MinValue
Exception Thrown: System.OverflowException

参考:


  • https://docs . Microsoft . com/en-us/dotnet/API/system . convert . toint 64?view = net framework-4 . 7 . 2 # System _ Convert _ toint 64 _ System _ String _ System _ IFormatProvider _


推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
author-avatar
展翅翱翔512
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有